// pages/receive/receive.js
const util = require('~/utils/util')
import baseteam from '~/utils/teamleader.js'
import baseuser from '~/utils/users.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectdate: '',
    selectdate_old: '',
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    show: false,
    // 时间控件

    optionsid: '',
    reqcpgg: '',
    pursueReason: '',

    // 地址
    mailingAddress: "地址",
    recipients: "名字",
    contactNumber: "手机号",
    province: "区域",
    // 地址
  },
  changeaddress() {
    wx.navigateTo({
      // url: '../address/address',
      url: '/pages/address/address',

    })
  },
  naveback() {
    setTimeout(() => {
      wx.navigateBack()
    }, 300);
  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },
  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  onInput(event) {
    console.log(event)
    // console.log(util.formatDate(event.detail))
    this.setData({
      currentDate: event.detail,
      selectdate: util.formatDate(event.detail),
      selectdate_old: util.formatDate_old(event.detail)
    });
    this.onClose()
  },

  async getaddress() {
    let resdata = await baseteam.addresslist()
    for (const defoort in resdata.data.Result.records) {
      if (resdata.data.Result.records[defoort].isDefault == 1) {
        this.setData({
          mailingAddress: resdata.data.Result.records[defoort].detailedAddress,
          recipients: resdata.data.Result.records[defoort].recipients,
          contactNumber: resdata.data.Result.records[defoort].phone,
          province: resdata.data.Result.records[defoort].province,
        })

      }
    }
  },
  async getpublic() {
    let reqdata = {
      id: this.data.optionsid
    }
    let datamsg = await baseuser.particulars(reqdata)
    if (datamsg.code == 200) {
      this.setData({
        detailsmsg: datamsg.data.Result.product,
        // bottommsg: datamsg.data.Result.recommend,
        // detailsswiper: datamsg.data.Result.product.material.split(","),
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      optionsid: options.id,
      reqcpgg: options.reqcpgg
    })
    this.getaddress()
    this.getpublic()
  },
  async addOrders() {
    if (this.data.selectdate_old === '') {
      wx.showToast({
        title: '未选择开团时间',
        icon: 'error',
        duration: 2000
      })
      return false
    }
    if (this.data.pursueReason === '') {
      wx.showToast({
        title: '未输入备注信息',
        icon: 'error',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '处理中, 请稍等..',
      mask: true
    })
    let reqdata = {
      "productId": this.data.optionsid,
      "pursueReason": this.data.pursueReason,
      "estimatedOpeningTime": this.data.selectdate_old,
      "specification": this.data.reqcpgg,
      "mailingAddress": this.data.mailingAddress,
      "recipients": this.data.recipients,
      "contactNumber": this.data.contactNumber,
      "province": this.data.province
    }
    let datamsg = await baseteam.ourders(reqdata)
    if (datamsg.code == 200) {
      wx.hideLoading()
      wx.showToast({
        title: '领样成功',
        icon: 'success',
        duration: 1600
      })
      setTimeout(() => {
        this.naveback()
      }, 1600);

    } else {
      wx.hideLoading()
      wx.showToast({
        title: datamsg.message,
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})